<template>
  <el-card>
    <el-form ref="form" label-width="80px" :inline="true">
      <el-form-item label="一级分类">
        <!-- value属性即为v-model收集的数据 -->
        <!-- change事件：选中值发生变化时触发 -->
        <!-- scene===0时启用 -->
        <el-select
          :disabled="scene === 0 ? false : true"
          placeholder="请选择一级分类"
          v-model="categoryStore.c1Id"
          @change="handleC1Change"
        >
          <el-option
            v-for="item in categoryStore.c1List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          :disabled="scene !== 0"
          placeholder="请选择二级分类"
          v-model="categoryStore.c2Id"
          @change="handleC2Change"
        >
          <el-option
            v-for="item in categoryStore.c2List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          :disabled="scene !== 0"
          placeholder="请选择三级分类"
          v-model="categoryStore.c3Id"
        >
          <el-option
            v-for="item in categoryStore.c3List"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
//引入组件挂载完毕方法
import { onMounted } from 'vue'
//引入分类相关的仓库
import useCategoryStore from '../../store/modules/category'

let categoryStore = useCategoryStore()
//组件挂载完毕,调用获取数据方法
onMounted(() => {
  getC1()
})
//调用仓库的获取一级分类数据方法
const getC1 = () => {
  categoryStore.getC1List()
}
//一级分类下拉菜单的change事件
const handleC1Change = () => {
  //需要重置后面级联分类的一些数据
  categoryStore.c2Id = ''
  categoryStore.c3Id = undefined
  categoryStore.c3List = []
  //调用仓库的获取二级分类数据方法
  categoryStore.getC2List()
}
//二级分类下拉菜单的change事件
const handleC2Change = () => {
  categoryStore.c3Id = undefined
  //调用仓库的获取三级分类数据方法
  categoryStore.getC3List()
}

//接收父组件传递过来的scene
defineProps(['scene'])
</script>

<style scoped>
.el-select {
  width: 200px;
}
</style>
